package com.asset.dispoal.channel.service.common;

import com.asset.dispoal.channel.domain.db.entity.SysChannel;
import com.asset.dispoal.channel.domain.db.entity.SysChannelDebt;
import com.asset.dispoal.channel.domain.db.mapper.SysChannelMapper;
import com.asset.dispoal.channel.dto.SysChannelDebtRequest;
import com.asset.dispoal.channel.dto.SysChannelRequest;
import com.asset.dispoal.channel.service.converter.SysChannelConverter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import tech.mhuang.pacebox.core.date.DateTimeUtil;
import tech.mhuang.pacebox.core.ddd.BaseCommand;
import tech.mhuang.pacebox.springboot.protocol.Result;

import java.math.BigDecimal;

/**
 * @author qr
 * @date 2022/3/25$
 */
@Component
public class SysChannelDebtUpdateCommand implements BaseCommand<SysChannelDebtRequest, Result> {

    @Autowired
    SysChannelMapper sysChannelMapper;

    @Override
    public Result execute(SysChannelDebtRequest request) {
        BigDecimal source = sysChannelMapper.getDebtMoney(request.getId());
        SysChannelDebt sysChannel = SysChannelConverter.INSTANCE.resDebtToDo(request);
        sysChannel.setUpdateTime(DateTimeUtil.currentDate());
        sysChannelMapper.updateDebt(sysChannel);
        sysChannelMapper.addTotalMoney(sysChannel.getMoney().subtract(source),request.getChannelId());
        return Result.ok();
    }
}
